/*
 * Copyright (C) 2015 Daniel Uppenkamp
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.maurersmarbles.drawables;

import com.google.common.base.Preconditions;
import com.googlecode.maurersmarbles.RGB;
import com.googlecode.maurersmarbles.Rectangle;
import com.googlecode.maurersmarbles.ScreenCanvas;

/**
 *
 * @author Daniel Uppenkamp
 */
public class ButtonDrawable implements Drawable {

    /**
     * The ratio of the button height to the arc height/width diameter.
     */
    protected static final float BUTTON_HEIGHT_TO_ARC_DIAMETER = 0.25f;
    /**
     * The ratio of the button height to the foreground height.
     */
    protected static final float BUTTON_HEIGHT_TO_FOREGROUND_HEIGHT = 0.90f;    
    /**
     * The ratio of the screen height to the button height.
     */
    public static final float SCREEN_HEIGHT_TO_BUTTON_HEIGHT = 0.04f;
    /**
     * The ratio of the screen width to the button width.
     */
    public static final float SCREEN_WIDTH_TO_BUTTON_WIDTH = 0.75f;
    /**
     * Whether or not the button is pressed.
     */
    protected boolean pressed = false;
    /**
     * The background color of the button.
     */
    protected int backgroundRgb = RGB.DARK_GRAY;
    /**
     * The foreground color of the button.
     */
    protected int foregroundRgb = RGB.LIGHT_GRAY;
    /**
     * The text color.
     */
    protected int textRgb = RGB.BLACK;
    /**
     * The rectangle to draw the button in.
     */
    protected Rectangle rectangle;
    /**
     * The text on the button.
     */
    protected String text;

    /**
     * Creates a new ButtonDrawable.
     *
     * @param text the text of the button
     */
    public ButtonDrawable(String text) {
        Preconditions.checkNotNull(text, "Null text");
        this.text = text;
    }

    /**
     * Sets the background color of the button.
     *
     * @param rgb the background color of the button
     */
    public void setBackgroundRgb(int rgb) {
        this.backgroundRgb = rgb;
    }

    /**
     * Sets the foreground color of the button.
     *
     * @param rgb the foreground color of the button
     */
    public void setForegroundRgb(int rgb) {
        this.foregroundRgb = rgb;
    }

    /**
     * Sets whether or not the button is pressed.
     *
     * @param pressed whether or not the button is pressed
     */
    public void setPressed(boolean pressed) {
        this.pressed = pressed;
    }

    /**
     * Returns the rectangle to draw the button in.
     *
     * @return the rectangle to draw the button in
     */
    public Rectangle getRectangle() {
        return rectangle;
    }

    /**
     * Sets the rectangle to draw the button in.
     *
     * @param rectangle the rectangle to draw the button in
     */
    public void setRectangle(Rectangle rectangle) {
        Preconditions.checkNotNull(rectangle, "Null rectangle");
        this.rectangle = rectangle;
    }

    /**
     * Returns the text of the button.
     *
     * @return the text of the button
     */
    public String getText() {
        return text;
    }

    /**
     * Sets the text color.
     *
     * @param textRgb the text color
     */
    public void setTextRgb(int textRgb) {
        this.textRgb = textRgb;
    }

    @Override
    public void draw(ScreenCanvas screenCanvas) {

        Preconditions.checkNotNull(screenCanvas, "Null screen canvas");
        
        int arcDiameter = (int) (rectangle.getHeight() * BUTTON_HEIGHT_TO_ARC_DIAMETER);
        if (pressed) {
            screenCanvas.fillRoundRect(rectangle, arcDiameter, arcDiameter, foregroundRgb);
        }
        else {
            screenCanvas.fillRoundRect(rectangle, arcDiameter, arcDiameter, backgroundRgb);
            screenCanvas.fillRoundRect(rectangle.getX(), rectangle.getY(), rectangle.getWidth(), (int) (rectangle.getHeight() * BUTTON_HEIGHT_TO_FOREGROUND_HEIGHT), arcDiameter, arcDiameter, foregroundRgb);
        }

        screenCanvas.drawStringCentered(text, rectangle, textRgb);

    }
    
}
